# Maintainer: Antonio Rojas <arojas@archlinux.org>

pkgbase=avogadrolibs
pkgname=(avogadrolibs avogadrolibs-qt5)
pkgver=1.98.1
pkgrel=3
pkgdesc='Libraries that provide 3D rendering, visualization, analysis and data processing useful in computational chemistry, molecular modeling, bioinformatics, materials science, and related areas'
arch=(loong64 x86_64)
url='https://www.openchemistry.org/'
license=(custom)
makedepends=(boost
             cmake
             eigen
             fmt
             hdf5
             genxrdpattern
             glew
             libmsym
             mmtf-cpp
             nlohmann-json
             openmpi
             pybind11
             python
             qt5-svg
             qt5-tools
             spglib
             tbb
             verdict
             vtk)
source=(https://github.com/OpenChemistry/avogadrolibs/archive/$pkgver/$pkgname-$pkgver.tar.gz)
sha256sums=('cb1d2c83a7f2c89c46ad46fdecace0d6f4de76c0898708ad52cf1e5a0aad1fb6')

prepare() {
  mkdir crystals molecules # Dummy dirs to trick cmake, actually provided by avogadro-molecules and avogadro-crystals
}

build() {
  cmake -B build -S $pkgname-$pkgver \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_C_FLAGS="$CFLAGS -ffat-lto-objects" \
    -DCMAKE_CXX_FLAGS="$CXXFLAGS -ffat-lto-objects" \
    -DENABLE_TRANSLATIONS=ON \
    -DUSE_HDF5=ON \
    -DUSE_VTK=ON \
    -DUSE_PYTHON=ON \
    -DUSE_SYSTEM_GENXRDPATTERN=ON \
    -DUSE_EXTERNAL_NLOHMANN=ON \
    -DUSE_EXTERNAL_PUGIXML=ON
  cmake --build build
}

package_avogadrolibs() {
  depends=(avogadro-crystals
           avogadro-molecules
           gcc-libs
           glew
           glibc
           hdf5
           libglvnd
           molequeue
           pugixml
           spglib)
  optdepends=('avogadrolibs-qt5: For the VTK and Qt plugins')

  DESTDIR="$pkgdir" cmake --install build
  rm -r "$pkgdir"/usr/lib/libAvogadroQt* \
        "$pkgdir"/usr/lib/libAvogadroMoleQueue* \
        "$pkgdir"/usr/lib/libAvogadroVtk* \
        "$pkgdir"/usr/include/avogadro/{molequeue,qt*,vtk} \
        "$pkgdir"/usr/lib/avogadro2/{scripts,staticplugins}

  install -Dm644 $pkgname-$pkgver/LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname
}

package_avogadrolibs-qt5() {
  pkgdesc="Qt 5 modules for Avogadro"
  depends=(avogadrolibs
           fmt # needed by VTK dependencies
           gcc-libs
           glew
           glibc
           libarchive
           libglvnd
           libmsym
           libxcursor # needed by VTK dependencies
           openmpi # needed by VTK dependencies
           qt5-base
           qt5-svg
           vtk)

  DESTDIR="$pkgdir" cmake --install build/avogadro/molequeue
  DESTDIR="$pkgdir" cmake --install build/avogadro/qtgui
  DESTDIR="$pkgdir" cmake --install build/avogadro/qtopengl
  DESTDIR="$pkgdir" cmake --install build/avogadro/qtplugins
  DESTDIR="$pkgdir" cmake --install build/avogadro/vtk

  install -Dm644 $pkgbase-$pkgver/LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname
}
